package cn.zyx.pet.service.impl;

import cn.zyx.basic.service.impl.BaseServiceImpl;
import cn.zyx.basic.util.*;
import cn.zyx.order.domain.PetAcquisitionOrder;
import cn.zyx.order.mapper.PetAcquisitionOrderMapper;
import cn.zyx.org.domain.Employee;
import cn.zyx.org.domain.Shop;
import cn.zyx.org.mapper.EmployeeMapper;
import cn.zyx.org.mapper.ShopMapper;
import cn.zyx.pet.domain.Pet;
import cn.zyx.pet.domain.PetDetail;
import cn.zyx.pet.domain.SearchMasterMsg;
import cn.zyx.pet.mapper.PetDetailMapper;
import cn.zyx.pet.mapper.PetMapper;
import cn.zyx.pet.mapper.SearchMasterMsgMapper;
import cn.zyx.pet.query.SearchMasterMsgQuery;
import cn.zyx.pet.service.ISearchMasterMsgService;
import cn.zyx.user.domain.User;
import cn.zyx.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper; // 收购订单

    @Override
    @Transactional
    public void publish(SearchMasterMsg searchMasterMsg,long loginInfoId) {
        // 找到最近的门店，绑定门店到寻主消息
        // 计算发布寻主消息人员的位置
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 找到最近的门店
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 绑定是哪个用户发送的消息 --> 利用我们封装的工具类先获取用户的用户信息 - controller层获取传过来
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        // 储存寻主的消息
        searchMasterMsgMapper.save(searchMasterMsg);
        // 发送短信提醒最近的店铺管理员
        // SmsUtils.sendSms(nearestShop.getTel(), "老板，你有新单【"+searchMasterMsg.getName()+"】，快去接客哦！");
        System.out.println("老板，你有新单【"+searchMasterMsg.getName()+"】，快去接客哦！");
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, Long loginInfoId) {
        // 声明是待处理
        searchMasterMsgQuery.setState(0);
        // 通过loginInfoId查询出当前employee员工
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        // 判断当前员工的shopId是否为空，不为空表示是店铺管理员，为空表示是平台管理员
        if (employee.getShop_id() != null){
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, Long loginInfoId) {
        // 声明是已处理
        searchMasterMsgQuery.setState(1);
        // 通过loginInfoId查询出当前employee员工
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        // 判断当前员工的shopId是否为空，不为空表示是店铺管理员，为空表示是平台管理员
        if (employee.getShop_id() != null){
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    public PageList<SearchMasterMsg> queryUser(SearchMasterMsgQuery searchMasterMsgQuery, Long loginInfoId) {
        // 平台用户登录
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsgQuery.setUserId(user.getId());
        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    @Transactional
    public void handle(Pet pet,Long loginInfoId) {
        // 修改寻主消息状态
        searchMasterMsgMapper.updateByStateForProcessed(pet.getSearch_master_msg_id());
        // 生成宠物基本信息 -- 返回宠物的自增id
        petMapper.save(pet);
        // 生成宠物详情
        PetDetail detail = pet.getDetail();
        if (detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
        // 生成收购订单
        // 获取当前员工的信息
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        // 获取当前寻主的消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        // 调用工具类生成订单的信息
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        // 把订单保存到数据库
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

    }

    /**
     * 生成收购订单的信息
     * @param pet
     * @param adopt
     * @param empId
     * @return
     */
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id()); // 订单唯一标识
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
